#include"player.h"

void initPlayer(void)
{

    /* Charge le sprite de notre héros */
    player.sprite = loadImage("graphics/walkright.png");

    /* Coordonnées de démarrage de notre héros */
    player.x = 10 ;//SCREEN_WIDTH/2 - PLAYER_WIDTH ;
    player.y = 460;

}

///////////////////////////////////////////


void Render (Hero* Sp,SDL_Surface* screen)
{
    SDL_Rect R,src ;

    R.x = (Sint16)Sp->x;
    R.y = (Sint16)Sp->y;
    R.w = PLAYER_WIDTH;
    R.h = PLAYER_HEIGTH;

    src.x = player.frameNumber * PLAYER_WIDTH ;
    src.y = 0;
    src.w = PLAYER_WIDTH;
    src.h = PLAYER_HEIGTH;

    SDL_BlitSurface(Sp->sprite,&src,screen,&R);
    SDL_Delay(1);

}



void Saute(Hero* Sp,float impulsion)
{
    Sp->vy = -impulsion;
    Sp->status = STAT_AIR;
}

float fonction(int x)
{
    int sol=586;
   /* if (collision(&player,p_obs))
        return sol-p_obs.h;
    else*/
        return  sol ;
}

void ControleSol(Hero* Sp)
{
    //printf("  \n %d ", collision());
    if ( Sp->y+PLAYER_HEIGTH > fonction(map.x[0]) )
    {
        Sp->y = fonction(map.x[0])-PLAYER_HEIGTH;
        if (Sp->vy>0 ) Sp->vy = 0.0f;

        Sp->status = STAT_SOL;
    }

}

void Gravite(Hero* Sp,float factgravite,float factsautmaintenu,Uint8* keys)
{
    if ( Sp->status == STAT_AIR && keys[SDLK_SPACE] )
        factgravite/=factsautmaintenu;

    Sp->vy += factgravite;
}

void Evolue(Hero* Sp,Uint8* keys)
{



    float lateralspeed = 4.0f;
    float airlateralspeedfacteur = 1.5f;
    float maxhspeed = 4.0f; //vitesse
    float adherance = 10.5f;
    float impulsion = 9.0f;
    float factgravite = 0.7f;
    float factsautmaintenu = 3.0f;



    Sp->etat=WALK;
// controle lateral
    if ( Sp->status == STAT_AIR ) // (*2)
        lateralspeed*= airlateralspeedfacteur;
    if ( keys[SDLK_LEFT] ) // (*1)
        Sp->vx-=lateralspeed;
    if ( keys[SDLK_RIGHT]  /*&& map.x[0]< map.background[0]->w-SCREEN_WIDTH */ )
        Sp->vx+=lateralspeed;
    if (Sp->status == STAT_SOL && !keys[SDLK_LEFT] && !keys[SDLK_RIGHT]) // (*3)
    {
        Sp->vx/=adherance;
        Sp->etat=STOP;
    }
// limite vitesse
    if (Sp->vx>maxhspeed) // (*4)
        Sp->vx = maxhspeed;
    if (Sp->vx<-maxhspeed)
        Sp->vx = -maxhspeed;
// saut
    if (keys[SDLK_SPACE] && Sp->status == STAT_SOL) Saute(Sp,impulsion);

    Gravite(Sp,factgravite,factsautmaintenu,keys);
    ControleSol(Sp);
// application du vecteur à la position.
  if ( Sp->x+PLAYER_WIDTH/2 >= SCREEN_WIDTH/2 && map.x[0] < map.background[0]->w-SCREEN_WIDTH )
        Sp->x= SCREEN_WIDTH/2-(PLAYER_WIDTH/2) ;
    if (Sp->x < 0 ) Sp->x=0;
   // if (Sp->x > map.x[0]-PLAYER_WIDTH )
   //     Sp->x=0;

    Sp->x +=Sp->vx;
    Sp->y +=Sp->vy;
}







